mod = 10 ** 9 + 7
t = int(input())
while t > 0:
t -= 1
n = int(input())
a = list(map(int, input().split()))
p = [0 for i in range(n)]
for i in range(n):
p[a[i]] = i
left = right = p[0]
ans = 1
for i in range(n-1):
if p[i+1] < left:
left = p[i+1]
elif p[i+1] > right:
right = p[i+1]
else:
ans = ans * (right - left - i) % mod
print(ans % mod)
#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
signed main()
{
int t;
cin>>t;
vector<int> factorial(100005,1);
for(int i=2;i<100005;i++) factorial[i]=(factorial[i-1]*i)%M;
while(t--)
{
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++) cin>>v[i];
vector<int> fixed(n,0), positionFixed(n,0);
vector<int> leftReverse(n), right(n);
int mini=n;
for(int i=0;i<n;i++)
{
leftReverse[i]=mini;
if(v[i]<mini)
{
mini=v[i];
fixed[mini]=1;
positionFixed[i]=1;
}
}
mini=n;
for(int i=n-1;i>=0;i--)
{
right[i]=mini;
if(v[i]<mini)
{
mini=v[i];
fixed[mini]=1;
positionFixed[i]=1;
}
}
vector<int> left(leftReverse);
reverse(left.begin(), left.end());
vector<int> prefixFixed(n);
prefixFixed[0]=positionFixed[0];
for(int i=1;i<n;i++) prefixFixed[i]=prefixFixed[i-1]+positionFixed[i];
// for(auto it:positionFixed) cout<<it<<" "; cout<<"\n";
// for(auto it:prefixFixed) cout<<it<<" "; cout<<"\n";
// for(auto it:leftReverse) cout<<it<<" "; cout<<"\n";
// for(auto it:left) cout<<it<<" "; cout<<"\n";
// for(auto it:right) cout<<it<<" "; cout<<"\n";
int ans=1, extraFixed=0;
for(int i=2;i<n;i++)
{
if(fixed[i]==1) continue;
int l = n - 1 - (upper_bound(left.begin(), left.end(), i) - left.begin() - 1);
int r = upper_bound(right.begin(), right.end(), i) - right.begin() - 1;
// cout<<l<<" "<<r<<"\n";
if(l<=r)
{
int spaces=r-l+1;
int spacesFixed = ((l==0) ? prefixFixed[r] : prefixFixed[r]-prefixFixed[l-1]) + extraFixed;
ans = (ans*(spaces-spacesFixed))%M;
}
extraFixed++;
}
cout<<ans<<"\n";
}
}
153. Find Minimum in Rotated Sorted Array | 150. Evaluate Reverse Polish Notation |
144. Binary Tree Preorder Traversal | 137. Single Number II |
130. Surrounded Regions | 129. Sum Root to Leaf Numbers |
120. Triangle | 102. Binary Tree Level Order Traversal |
96. Unique Binary Search Trees | 75. Sort Colors |
74. Search a 2D Matrix | 71. Simplify Path |
62. Unique Paths | 50. Pow(x, n) |
43. Multiply Strings | 34. Find First and Last Position of Element in Sorted Array |
33. Search in Rotated Sorted Array | 17. Letter Combinations of a Phone Number |
5. Longest Palindromic Substring | 3. Longest Substring Without Repeating Characters |
1312. Minimum Insertion Steps to Make a String Palindrome | 1092. Shortest Common Supersequence |
1044. Longest Duplicate Substring | 1032. Stream of Characters |
987. Vertical Order Traversal of a Binary Tree | 952. Largest Component Size by Common Factor |
212. Word Search II | 174. Dungeon Game |
127. Word Ladder | 123. Best Time to Buy and Sell Stock III |